package com.iap.admin.view.vector.code
{
	import com.iap.admin.view.vector.VectorPreviewControl;
	import com.iap.common.model.Filler;
	import com.iap.common.model.SampleFiller;
	import com.iap.common.model.VectorImage;
	import com.iap.common.view.control.VectorImageFillerDisplay;
	
	import mx.controls.SWFLoader;
	
	import spark.components.Group;
	
	public class VectorPreviewControlCode extends Group
	{
		protected var view:VectorPreviewControl;
		
		protected var model:Array;

		protected var sampleFillers:Array;
		
		public function VectorPreviewControlCode()
		{
			super();
		}
		
		public function setSource(model:Array, sampleFillers:Array = null):void
		{
			this.model = model;
			this.sampleFillers = sampleFillers;
			clearImages();
			addImages();
		}
		
		protected function init(view:VectorPreviewControl):void
		{
			this.view = view;
		}

		private function addImages():void
		{
			for each(var item:VectorImage in model)
			{
				item.layer
				addImage(item, getSampleFillerByLayer(item.layer));
			}
		}
		
		private function getSampleFillerByLayer(layer:uint):SampleFiller
		{
			var result:SampleFiller = null;
			for each(var item:SampleFiller in sampleFillers)
			{
				if(item.layer == layer)
					result = item;
			}
			return result;
		}
		
		private function clearImages():void
		{
			view.main.removeAllElements();
		}
		
		private function addImage(item:VectorImage, sampleFiller:SampleFiller = null):void
		{
			var control:VectorImageFillerDisplay = new VectorImageFillerDisplay();
			var filler:Filler = sampleFiller ? sampleFiller.filler : null;
			control.width = width;
			control.height = width;
			control.setSource(item, filler);
			view.main.addElement(control);
		}
		
		public function setSampleFiller(sampleFiller:SampleFiller):void
		{
			var filler:Filler = sampleFiller.filler;
			var layer:uint = sampleFiller.layer;
			var idx:uint = layer>0 ? layer-1 : 0; 
			if(view.main.getElementAt(idx) == null) return;
			
			var control:VectorImageFillerDisplay = view.main.getElementAt(idx) as VectorImageFillerDisplay;
			control.setFiller(filler);
		}
	}
}